﻿using Models;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace VirtualGameDAL
{
    public static partial class RoleService
    {
        /// <summary>
        /// 添加新角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns>role</returns>
        public static Role AddRole(Role role)
        {
            string sql =
                "INSERT Role (name,originalLife,currentLife,damage,agile,brains,speed,currentLocationX,currentLocationY,originaLocationX,originaLocationY,weaponId,type) "+
                "VALUES (@name,@originalLife,@currentLife,@damage,@agile,@brains,@speed,@currentLocationX,@currentLocationY,@originaLocationX,@originaLocationY,@weaponId,@type)";
            sql += " ; SELECT @@IDENTITY";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@name", role.Name), 
				new SqlParameter("@originalLife", role.OriginalLife),
				new SqlParameter("@currentLife", role.CurrentLife),
                new SqlParameter(",@damage", role.Damage),
                new SqlParameter(",@agile", role.Agile),
                new SqlParameter("@brains", role.Brains),
                new SqlParameter("@speed", role.Speed),
                new SqlParameter("@currentLocationX", role.CurrentLocation.X),
                new SqlParameter("@currentLocationY", role.CurrentLocation.Y),
                new SqlParameter("@originaLocationX", role.OriginaLocation.X),
                new SqlParameter("@originaLocationY", role.OriginaLocation.Y),
                new SqlParameter("@weaponId", role.Wpn.Id),//FK
                new SqlParameter("@type", role.Type),
            };
            int newId = DBHelper.GetScalar(sql, para);
            return GetRoleById(newId);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="role"></param>
        public static void DeleteRoler(Role role)
        {
            DeleteRolerById(role.Id);
        }

        /// <summary>
        /// 根据id删除角色
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteRolerById(int id)
        {
            string sql = "DELETE FROM Role WHERE id = @id";
            SqlParameter[] para = new SqlParameter[]
              {
                new SqlParameter("@id", id)
              };
            DBHelper.ExecuteCommand(sql, para);
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="role"></param>
        public static void ModifyRole(Role role)
        {
            string sql =
                "UPDATE Role " +
                "SET " +
                    "name = @name, " +
                    "originalLife = @originalLife, " +
                    "currentLife = @currentLife, " +
                    "damage = @damage, " +
                    "agile = @agile, " +
                    "brains = @brains, " +
                    "speed = @speed, " +
                    "currentLocationX = @currentLocationX, " +
                    "currentLocationY = @currentLocationY, " +
                    "originaLocationX = @originaLocationX, " +
                    "originaLocationY = @originaLocationY, " +
                    "weaponId = @weaponId, " +
                    "type = @type " +
                "WHERE id = @id";
            SqlParameter[] para = new SqlParameter[]
              {
                new SqlParameter("@id", role.Id),
                new SqlParameter("@name", role.Name),
                new SqlParameter("@originalLife", role.OriginalLife),
                new SqlParameter("@currentLife", role.CurrentLife),
                new SqlParameter(",@damage", role.Damage),
                new SqlParameter(",@agile", role.Agile),
                new SqlParameter("@brains", role.Brains),
                new SqlParameter("@speed", role.Speed),
                new SqlParameter("@currentLocationX", role.CurrentLocation.X),
                new SqlParameter("@currentLocationY", role.CurrentLocation.Y),
                new SqlParameter("@originaLocationX", role.OriginaLocation.X),
                new SqlParameter("@originaLocationY", role.OriginaLocation.Y),
                new SqlParameter("@weaponId", role.Wpn.Id),//FK
                new SqlParameter("@type", role.Type)
              };
            DBHelper.ExecuteCommand(sql, para);
        }
        /// <summary>
        /// 查询所有角色
        /// </summary>
        /// <returns></returns>
        public static IList<Role> GetAllRoles()
        {
            string sqlAll = "SELECT * FROM Role";
            return GetRolesBySql(sqlAll);
        }

        /// <summary>
        /// 根据类型查询角色
        /// </summary>
        /// <returns></returns>
        public static IList<Role> GetAllFriends()
        {
            string sqlAll = "SELECT * FROM Role WHERE type=0 or type=1";
            return GetRolesBySql(sqlAll);
        }

        /// <summary>
        /// 根据类型查询角色
        /// </summary>
        /// <returns></returns>
        public static IList<Role> GetAllEnemies()
        {
            string sqlAll = "SELECT * FROM Role WHERE  type=2 or type=3";
            return GetRolesBySql(sqlAll);
        }

        /// <summary>
        /// 根据id查询单个角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Role GetRoleById(int id)
        {
            string sql = "SELECT * FROM Role WHERE id = @id";

            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))//使用Using语句，资源可以得到及时释放
            {
                if (reader.Read())
                {
                    Role role = new Role();
                    role.Id = (int)reader["id"];
                    role.Name = (string)reader["name"];
                    role.OriginalLife = (int)reader["originalLife"];
                    role.CurrentLife = (int)reader["currentLife"];
                    role.Damage = (int)reader["damage"];
                    role.Agile = (int)reader["agile"];
                    role.Brains = (int)reader["brains"];
                    role.Speed = (int)reader["speed"];
                    role.CurrentLocation = new Point((int)reader["currentLocationX"], (int)reader["currentLocationY"]);
                    role.OriginaLocation = new Point((int)reader["originaLocationX"], (int)reader["originaLocationY"]);
                    int weaponId = (int)reader["weaponId"];
                    role.Wpn = WeaponService.GetWeaponById(weaponId);//外键的处理
                    role.Type = (int)reader["type"];

                    reader.Close();//注意关闭reader
                    return role;
                }
                else
                {
                    reader.Close();//没有记录时，也需要关闭reader
                    return null;
                }
            }
        }

        /// <summary>
        /// 依据sql语句查询角色
        /// </summary>
        /// <param name="safeSql"></param>
        /// <returns></returns>
        private static IList<Role> GetRolesBySql(string safeSql)
        {
            List<Role> list = new List<Role>();
            int weaponId;
            using (DataTable table = DBHelper.GetDataSet(safeSql))
            {
                foreach (DataRow row in table.Rows)
                {
                    Role role = new Role();
                    role.Id = (int)row["id"];
                    role.Name = (string)row["name"];
                    role.OriginalLife = (int)row["originalLife"];
                    role.CurrentLife = (int)row["currentLife"];
                    role.Damage = (int)row["damage"];
                    role.Agile = (int)row["agile"];
                    role.Brains = (int)row["brains"];
                    role.Speed = (int)row["speed"];
                    role.CurrentLocation = new Point((int)row["currentLocationX"], (int)row["currentLocationY"]);
                    role.OriginaLocation = new Point((int)row["originaLocationX"], (int)row["originaLocationY"]);
                    weaponId = (int)row["weaponId"];
                    role.Wpn =WeaponService.GetWeaponById(weaponId);//外键的处理
                    role.Type = (int)row["type"];

                    list.Add(role);
                }
                return list;
            }
        }

    }
}
